"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : gs_ctl restart只使用-D指定不正确数据库实例目录启动主机
Create At   : 2021/01/18
Owner       : opentestcase015
Description :
    1.查看集群状态是否正常
    2.指定-D设置参数为不正确的数据库实例目录，restart启动主机
    3.查看集群状态主机是否正常
Expect      :
    1.查看集群状态正常成功
    2.指定-D设置参数为不正确的数据库实例目录，restart失败
    3.查看集群状态主机状态为正常
History     :
    Modified by opentestcase042 2023/3/24: 优化用例,修改用例标题
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.env_path = macro.DB_ENV_PATH
        self.my_path = macro.DB_BACKUP_PATH
        self.user_node = Node('PrimaryDbUser')

    def test_system_internal_tools(self):
        LOG.info('----查看集群状态-------')
        status = primary_sh.get_db_cluster_status()
        self.assertTrue(status)

        LOG.info('----重启数据库主机-------')
        excute_cmd = f'''
                    source {self.env_path};
                    gs_ctl restart -D {self.my_path} -M primary  ;
                        '''
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn(self.constant.read_file_error, msg)

        LOG.info('----查看集群状态-------')
        status = primary_sh.get_db_cluster_status()
        self.assertTrue(status)

    def tearDown(self):
        LOG.info('----this is tearDown------')
        LOG.info('----恢复集群状态------')
        is_start = primary_sh.start_db_cluster()
        LOG.info(is_start)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')
